// pages/forwarding/forwarding.js
const app = getApp();
let Api = require('../../api/api.js');
let util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        consignee:'',//收件人
        name: '',
        indicatorDots: true,
        duration: 1000,
        interval: 5000,
        circular:true,
        detailBanner: [],
        nameplate:'',
        indicatorColor:'rgba(255,255,255,1)',
        gift:[],
        orderType:'普通订单',
        ordernumber:'',
        shareuserid:'',
        phone: '13255555555',
        area: '浙江省杭州市上城区石祥路1号',
        isOrder: '', // True  礼物转发者是自己    False  礼物转发者不是自己 
        isReceiveOrder: '',// True  礼物接受者是自己    False  礼物接受者不是自己
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let self = this;
        let tokenValue = wx.getStorageSync('token');
        app.globalData.token = tokenValue;
        console.log('----------app.globalData.token----------------', app.globalData.token)
        if (tokenValue === undefined || tokenValue === "" || tokenValue === null) {
            wx.login({
                success: res => {
                    wx.setStorageSync('code', res.code)
                    //重新授权
                    wx.redirectTo({
                        url: '/pages/auth/auth?loginType=forwarding&orderType=' + options.orderType + '&ordernumber=' + options.ordernumber + '&shareuserid=' + options.shareuserid
                    })
                }
            })
            
        } else {
            self.setData({
                orderType: options.orderType,
                ordernumber: options.ordernumber,
                shareuserid: options.shareuserid,
            })
            //18）我的订单列表
            Api.get(app.globalData.mallDomain, {
                doShowMyOrderList: 1,
                status:1, 
                token: app.globalData.token,
                ordernumber: options.ordernumber,
            }).then(res => {
                console.log(res, '礼物查收订单列表')
                let giftInfo = [];
                res.data.list[0].list.forEach(ele => {
                    giftInfo.push({
                        wpCover: ele.wpCover,
                        name: unescape(ele.wpName),
                        customvalue: ele.customvalue,
                        IsOrder: ele.IsOrder,
                        IsReceiveOrder: ele.IsReceiveOrder, 
                    })
                })
                self.setData({
                    detailBanner: giftInfo,
                    name: giftInfo[0].name,
                    nameplate: giftInfo[0].customvalue,
                    consignee: res.data.list[0].name,
                    phone: res.data.list[0].phone,
                    area: res.data.list[0].address,
                    isOrder: res.data.list[0].IsOrder,
                    isReceiveOrder: res.data.list[0].IsReceiveOrder,
                })
            })
        }
    },
    /**
     * 点击填写地址按钮
     */
    addAddress: function () {
        let self = this;
        wx.navigateTo({
            url: '../address/address?addressType=share&shareuserid=' + self.data.shareuserid + '&ordernumber=' + self.data.ordernumber
        })
    },
    /**
     * 确认收货
     */
    confirmGoods: function () {
        let self = this;
        Api.get(app.globalData.mallDomain, { //19）修改订单状态 
            doChangeOrderStatus: 1,
            token: app.globalData.token,
            status: 5, //已完成
            ordernumber: self.data.ordernumber,
        }).then(res => {
            if (res.data.msg == '118') {
                self.setData({
                    tipText: '收货完成！',
                    showToast: true,
                })
                setTimeout(function () {
                    self.setData({
                        tipText: '',
                        showToast: false
                    })
                }, 1000)
            }
            console.log(res)
        })
    },
    /**
     * 监听页面滑动
     */
    listeningSwiper: function (e) {
        let self = this;
        self.setData({
            name: self.data.detailBanner[e.detail.current].name,
            nameplate: self.data.detailBanner[e.detail.current].customvalue,
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let self = this;
        // orderType  自定义订单 ， 普通订单 两种类型
        return {
            title: self.data.giftInfo[0].name,
            path: 'pages/forwarding/forwarding?shareuserid=' + self.data.shareuserid + '&status=' + self.data.status +'&ordernumber=' + self.data.ordernumber + '&orderType=' + self.data.orderType,
            imageUrl: self.data.detailBanner[0].wpCover,
            success: function (res) {
                wx.showToast({
                    title: '分享成功',
                    icon: 'success',
                    duration: 2000
                })
            },
            fail: function (res) {
                console.log("转发失败:" + JSON.stringify(res));
            }
        }
    },
    
})